const api = require("../../../../utils/cloudAPIUtils");
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
      tradeNo:null,
      pwdStr:null,
      timer:null,
      times:15
  },

  _loadPayResult:function(){
      let tradeNo = this.data.tradeNo;
      const _self = this;
      api.post(api.url.pclResult,{
        tradeNo:tradeNo,
        payType:3
      },res=>{
          if(res.data.code == 200){
            if(res.data.body.resultCode == 2){
                if(_self.data.timer){
                    clearInterval(_self.data.timer);
                    _self.data.timer = null;
                }
                
                _self.setData({
                    pwdStr:res.data.body.pwdStr
                })
            }
          }else if(res.data.msg){
              wx.showModal({
                content: res.data.msg,
                showCancel:false,
                complete: (res) => {
                    wx.reLaunch({
                      url: '/pages/index/index',
                    })
                }
              })
          }else{
              wx.showToast({
                title: '获取信息失败',
                icon:'error'
              })
              setTimeout(function(){
                  wx.reLaunch({
                    url: '/pages/index/index',
                  })
              });
          }
        
      })
  },

  btnChangePwd:function(){
    const _self = this;
    wx.showModal({
        title:'温馨提示',
        content: '是否要切换密码？请确定已输入了当前密码',
        complete: (res) => {
            if (res.confirm) {
                api.post(api.url.pclChangePwd,{
                    tradeNo:_self.data.tradeNo
                },res=>{
                      if(res.data.code==200){
                        wx.showToast({title: '切换成功',icon:'none'})
                        _self.setData({pwdStr:res.data.body.pwdStr})
                      }else if(res.data.msg){
                        wx.showModal({
                            content: res.data.msg,
                            showCancel:false
                          })
                      }else {
                        wx.showToast({title:'切换失败',icon:'error'})
                      }
                })
            }
        }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.data.tradeNo = options.tradeNo;
      const _self = this;
      this.data.timer = setInterval(()=>{
          _self._loadPayResult();
          _self.data.times = _self.data.times  - 1;
          if(_self.data.times < 0){
            clearInterval(_self.data.timer);
            wx.showModal({
                content: '支付无结果，请重新扫码或联系客服',
                showCancel:false,
                complete: (res) => {
                    wx.reLaunch({
                      url: '/pages/index/index',
                    })
                }
            })
          }
      },1000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
      if(this.data.timer){
        clearInterval(this.data.timer);
        this.data.timer = null;
      }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})